package com.hank.activemq.springboot;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;

import org.apache.activemq.ActiveMQConnectionFactory;

public class MQProducer {
	public static void main(String[] args) throws JMSException, InterruptedException {
		// 连接到ActiveMQ服务器
		ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://server1.com:61616");
        String brokerURL="failover:(tcp://server1.com:61616,tcp://server1.com:61617,tcp://server1.com:61618)?randomize=false";
//        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(brokerURL);

        Connection connection = factory.createConnection();
		connection.start();
		Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
		// 创建主题
		Topic topic = session.createTopic("VirtualTopic.TEST");
		MessageProducer producer = session.createProducer(topic);
		//  NON_PERSISTENT 非持久化 PERSISTENT 持久化,发送消息时用使用持久模式
//		producer.setDeliveryMode(DeliveryMode.PERSISTENT);
		TextMessage message = session.createTextMessage();
/**
		int num=0;
		while(num>1000){
            num++;
            message.setText("topic 消息。"+num);
            message.setStringProperty("property", "消息Property");
            //  发布主题消息
            producer.send(message);
            Thread.sleep(1000);
        }
 **/
		for (int i = 0; i < 10; i++) {
			message.setText("topic 消息。"+i);
			message.setStringProperty("property", "消息Property");
			//  发布主题消息
			producer.send(message);
			System.out.println("发送消息:>>> topic 消息。"+i);
		}
		System.out.println("消息发送完毕!");

		session.close();
		connection.close();
	}

}
